package it.unical.mat.aec.gui;

import it.unical.mat.aec.manageResult.ManageResultFacade;
import it.unical.mat.aec.model.Domanda;
import it.unical.mat.aec.model.Studente;
import it.unical.mat.aec.model.Test;
import it.unical.mat.aec.persistence.FileManagerException;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EventObject;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.event.CellEditorListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;

public class VisualizzaTestPanel extends JPanel {

	private JTable table;
	private int IdTest;

	public VisualizzaTestPanel(int IdTest) {
		super(new BorderLayout());
		this.IdTest = IdTest;
		JSplitPane pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		pane.setOneTouchExpandable(false);
		table = new JTable(new TestManageTableModel(ManageResultFacade
				.getInstance().getTests().get(IdTest)));
		table.setDefaultRenderer(Object.class, new JTableRender());
		pane.add(table);
		pane.add(new JScrollPane(table), JSplitPane.RIGHT);
		JPanel upPanel = new JPanel(new BorderLayout());
		JPanel panel = new JPanel();
		upPanel.add(panel, BorderLayout.WEST);
		pane.add(upPanel, JSplitPane.LEFT);
		addComponent(panel);
		add(pane, BorderLayout.CENTER);
		
		JPanel southPanel=new JPanel(new BorderLayout());
		Studente studente=ManageResultFacade.getInstance().getTests().get(IdTest).getStudente();
		southPanel.add(new JLabel("Test di "+studente.getCognome()+" "+studente.getNome()),BorderLayout.WEST);
		add(southPanel,BorderLayout.SOUTH);
	}

	private void addComponent(JPanel panel) {
		Dimension dimension=new Dimension(80, 80);
		panel.add(IconButton.getLabelButton("images/icon/cancellaTest.png",
				dimension, "Cancella", new LabelButtonEvent() {

					@Override
					public void event() {
						try {
							if (ManageResultFacade.getInstance().getPathFile() != null)
								ManageResultFacade.getInstance().cancellaTest(
										IdTest,
										ManageResultFacade.getInstance()
												.getPathFile());
							else
								ManageResultFacade.getInstance().getTests()
										.remove(IdTest);
							MainFrame.getInstance().modificaPanel();

						} catch (FileManagerException e) {
							JOptionPane.showMessageDialog(
									VisualizzaTestPanel.this, e.getMessage(),
									"Errore", JOptionPane.ERROR_MESSAGE);
						}

					}
				}));
		panel.add(IconButton.getLabelButton("images/icon/indietro.png", dimension, "Indietro", new LabelButtonEvent() {
			
			@Override
			public void event() {
				MainFrame.getInstance().modificaPanel();
				
			}
		}));

	}

	class JTableRender extends DefaultTableCellRenderer {
		public Component getTableCellRendererComponent(JTable table,
				Object value, boolean isSelected, boolean hasFocus, int row,
				int column) {
			JLabel parent = (JLabel) super.getTableCellRendererComponent(table,
					value, isSelected, hasFocus, row, column);
			setHorizontalAlignment(JLabel.CENTER);
			parent.setFont(parent.getFont().deriveFont(Font.BOLD));
			if (column == table.getColumnCount() - 1) {
				parent.setFont(parent.getFont().deriveFont(Font.ITALIC));
			}
			if(value instanceof IconImage){
				IconImage img=(IconImage) value;
				parent.setIcon(img.getIcon());
				parent.setText(null);
			}else
				parent.setIcon(null);				
			return parent;
		}

	}
}
